package chances.wechat.ctrl;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;

import chances.epg.commons.utils.SpringWebUtils;
import chances.epg.session.UserSessionInterceptor;
import chances.wechat.config.WeChatMvcConfig;

public class WeChatSessionInterceptor extends UserSessionInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		WeChatMvcConfig config = SpringWebUtils.findBean(request, WeChatMvcConfig.class);
		ServletContext context = request.getServletContext();
		if(context.getAttribute("wechatConfig") == null){
			context.setAttribute("wechatConfig", config);
		}
		if (HandlerMethod.class.isInstance(handler)) {
			HandlerMethod aHanlderMeethod = (HandlerMethod) handler;
			if (WechatLoginController.class.isInstance(aHanlderMeethod.getBean())) {
				return true;
			}else{
				return super.preHandle(request, response, handler);
			}
		}else{
			return super.preHandle(request, response, handler);
		}
	}
	
}
